<template>
  <div>
    <div class="mask" v-if="showMask" @click="showIt"></div>
    <div class="cart">
      <div class="p">
        <div class="product">
          <div class="product__header" v-if="showCart">
            <div class="product__header__selectAll">
              <span class="icon iconfont">&#xe627;</span>
              全选
            </div>
            <div class="product__header__clearAll" @click="clearAll">
              清空购物车
            </div>
          </div>
          <div v-for="item in contentList" :key="item._id">
            <template>
              <div v-if="item.count > 0 && showCart" class="product__item">
                <span class="icon iconfont">&#xe8bd;</span>

                <img class="product__img" :src="item.imgUrl" alt="" />
                <div class="product__detail">
                  <h4 class="product__detail__title">{{ item.name }}</h4>
                  <p class="product__detail__price">
                    <span class="price">￥{{ item.price }}</span>
                    <span class="origin">￥{{ item.oldPrice }}</span>
                  </p>
                </div>
                <div class="product__number">
                  <span
                    class="product__number__minus iconfont"
                    @click="minus(shopId, item._id, item)"
                    >&#xe8b1;</span
                  >
                  {{ cart?.[shopId]?.[item._id]?.count || 0 }}
                  <span
                    class="product__number__plus iconfont"
                    @click="plus(shopId, item._id, item)"
                    >&#xe781;</span
                  >
                </div>
              </div>
            </template>
          </div>
        </div>
      </div>
      <div class="cart__check">
        <div class="cart__check__basket iconfont" @click="showIt">&#xecad;</div>
        <div class="tag">{{ getTag }}</div>
        <div class="cart__check__result">
          <span>总计:</span>
          <span class="cart__check__result__red">￥{{ getPrice }}</span>
          <div class="result" @click="goResult">去结算</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Cart",
  props: ["shopId"],
  data() {
    return {
      cart: this.$store.state.cart,
      showCart: false,
      showMask: false,
    };
  },
  computed: {
    getTag: {
      get() {
        let count = 0;
        count = this.$store.state.allcount;
        return count;
      },
    },
    getPrice() {
      let price = 0;
      price = this.$store.state.allprice.toFixed(1);
      return price;
    },
    contentList() {
      let cart = {};
      cart = this.$store.state.cart[this.shopId] || {};
      // console.log(cart);
      return cart;
    },
  },
  methods: {
    goResult(){this.$router.push({
      name:'order',
      params:{shopId:this.shopId}
    })},
    showIt() {
      this.$store.state.allcount &&
        (this.showCart = !this.showCart) &&
        (this.showMask = !this.showMask);
    },
    clearAll() {
      this.$store.commit("CLEARALL", this.shopId);
      this.showCart = !this.showCart;
      this.showMask = !this.showMask;
    },
    minus(shopId, productId, product) {
      this.$store.commit("MINUS", { shopId, productId, product });
    },
    plus(shopId, productId, product) {
      this.$store.commit("PLUS", { shopId, productId, product });
    },
  },
};
</script>

<style lang="scss" scoped>
.mask {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  top: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9;
}
.p {
  display: flex;
  position: absolute;
  bottom: 0.5rem;
  left: 0;
  right: 0;
  background-color: #fff;
}
.icon {
  font-size: 0.16rem;
}
.product__item .icon {
  position: absolute;
  left: -0.32rem;
  top: 0.28rem;
  color: #4fb0f9;
}
.product {
  overflow-y: scroll;
  flex: 1;
  &__header {
    display: flex;
    line-height: 0.52rem;
    border-bottom: 0.01rem solid #f1f1f1;
    &__selectAll {
      width: 0.64rem;
      margin-left: 0.22rem;
      font-size: 0.14rem;
      color: #333333;
    }
    &__clearAll {
      position: absolute;
      width: 0.98rem;
      right: 0.18rem;
      text-align: right;
      font-size: 0.14rem;
      color: #333;
    }
  }
  &__item {
    position: relative;
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0 0 0.54rem;
    border-bottom: 0.01rem solid #f1f1f1;
  }
  &__img {
    height: 0.46rem;
    width: 0.46rem;
    margin-right: 0.16rem;
  }
  &__detail {
    &__title {
      font-size: 0.14rem;
      color: #333333;
      margin: 0;
    }

    &__price {
      margin: 0;
      margin-top: 0.3rem;
    }
  }
  &__number {
    position: absolute;
    right: 0.18rem;
    bottom: 0.12rem;
    font-size: 0.18rem;
    &__minus {
      font-size: 0.2rem;
    }
    &__plus {
      font-size: 0.2rem;
    }
  }
}
.price {
  font-size: 0.14rem;
  color: #e93b3b;
  line-height: 0.2rem;
}
.origin {
  font-size: 0.1rem;
  color: #999999;
  text-decoration: line-through;
  line-height: 0.2rem;
  margin-left: 0.12rem;
}
.tag {
  position: absolute;
  left: 0.5rem;
  top: 0.01rem;
  min-width: 0.2rem;
  height: 0.2rem;
  text-align: center;
  line-height: 0.2rem;
  border-radius: 0.1rem;
  font-size: 0.15rem;
  transform: scale(0.7, 0.7);
  background-color: red;
  color: #fff;
}
.cart {
  position: absolute;
  left: 0;
  right: 0;
  height: 0.5rem;
  bottom: 0;
  background-color: #fff;
  z-index: 99;

  &__check {
    display: flex;
    position: relative;
    &__basket {
      font-size: 0.26rem;
      line-height: 0.5rem;
      width: 0.76rem;
      text-align: center;
      background: #fefdfdea;
      box-shadow: 0 -0.01rem 0.01rem 0 #f1f1f1;
      color: #4fb0f9;
    }
    &__result {
      flex: 1;
      line-height: 0.5rem;
      font-size: 0.12rem;
      color: #333333;
      padding-left: 0.08rem;
      &__red {
        font-size: 0.18rem;
        color: #e93b3b;
      }
    }
  }
}
.result {
  float: right;
  height: 0.5rem;
  width: 0.98rem;
  line-height: 0.5rem;
  text-align: center;
  background: #4fb0f9;
  font-size: 0.14rem;
  color: #ffffff;
}
</style>